<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-18
 * Time: 9:02
 */

namespace app\admin\controller;


use app\libs\Utils;
use app\model\AdminTenantConfig;
use app\util\ReturnCode;

class Tenant extends Base
{

    //编辑官方客服联系方式
    public function editCustomerService(){
        $customerServicePhone = $this->request->post('phone');
        $customerServiceQrcode = $this->request->post('qrcode');
        $customerServiceWxNo = $this->request->post('wx_no');
        if(Utils::isEmpty($customerServicePhone)||!Utils::isMobile($customerServicePhone)){
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS,'手机号不正确');
        }
        if(Utils::isEmpty($customerServiceQrcode)){
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS,'微信二维码不能为空');
        }
        if(Utils::isEmpty($customerServiceWxNo)){
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS,'微信号不能为空');
        }
        $model = (new AdminTenantConfig())->get($this->getUserTid());
        $model->setAttr('customer_service_phone',$customerServicePhone);
        $model->setAttr('customer_service_qrcode',$customerServiceQrcode);
        $model->setAttr('customer_service_wx_no',$customerServiceWxNo);
        $done = $model->allowField(true)->save();
        if($done){
            return $this->buildSuccess(null);
        }
        return $this->buildFailed(ReturnCode::FAILED,'修改失败');
    }
    //官方客服联系方式
    public function getCustomerService(){

        $model = (new AdminTenantConfig())->get($this->addTid([]));
        if($model){
            return $this->buildSuccess(['customer_service_phone'=>$model->customer_service_phone,'customer_service_wx_no'=>$model->customer_service_wx_no,'customer_service_qrcode'=>$model->customer_service_qrcode]);
        }
        return $this->buildFailed(ReturnCode::FAILED,'获取失败');
    }
    public function getTabbar(){
        $model = (new AdminTenantConfig())->get($this->addTid([]));
        if($model){
            return $this->buildSuccess(['tabbar'=>$model->tabbar]);
        }
        return $this->buildFailed(ReturnCode::FAILED,'获取失败');
    }
    public function editTabbar(){
        $tabbarData= $this->request->post('tabbar_data');
        $model = (new AdminTenantConfig())->get($this->getUserTid());
        $model->setAttr('tabbar',json_encode($tabbarData));
        $done = $model->allowField(true)->save();
        if($done){
            return $this->buildSuccess(null);
        }
        return $this->buildFailed(ReturnCode::FAILED,'修改失败');
    }
}